<template>
    <uni-popup ref="popup" type="bottom" @maskClick="close">
        <view class="popup-bottom">
            <view class="popup-body">
                <view class="tab">
                    <view class="tab-left">
                        <view @click="changeTab(index)" :class="['tab-item', { 'tab-active': active === index }]" v-for="(item, index) in list" :key="index">{{ item.name }}</view>
                    </view>
                </view>
                <swiper @animationfinish="changeSwiper" @change="changeTab($event.detail.current)" :current="current" class="swiper">
                    <swiper-item class="swiper-item" v-for="(item, index) in list" :key="index">
                        <scroll-view scroll-y class="scroll">
                            <view class="list">
                                <view @click="changeItem(item, key)" :class="['list-item', { active: item.active == key }]" v-for="(val, key) in item.gifts" :key="key">
                                    <view class="list-item__head">
                                        <image class="list-item__img" :src="static_url + val.icon_url" mode="heightFix"> </image>
                                    </view>
                                    <view class="list-item__body">
                                        <view class="list-item__name">{{ val.name }}</view>
                                        <view class="list-item__footer">
                                            <image src="/static/img/zs.png" class="zuanshi-icon" />
                                            <text class="price">{{ filterNum(val.price) }}</text>
                                        </view>
                                        <view class="give-btn" @click="give(val)">赠送</view>
                                    </view>
                                </view>
                            </view>
                        </scroll-view>
                    </swiper-item>
                </swiper>
            </view>
        </view>
    </uni-popup>
</template>

<script>
export default {
    data() {
        return {
            active: 0,
            current: 0,
            list: [],
            playList: [],
            static_url: process.env.VUE_APP_FILE_URL
        }
    },
    computed: {
        filterNum() {
            return price => Number(((price ?? 0) / 10).toFixed(2))
        }
    },
    mounted() {
        this.loadData()
    },
    methods: {
        async loadData() {
            let list = await this.$api.giftList({ activity_type: 1, type: 0, free: 1 })
            list.forEach(item => {
                item.active = item.gifts.length > 0 ? 0 : -1
            })
            this.list = list
        },
        open() {
            this.$refs.popup.open()
        },
        close() {
            this.$refs.popup.close()
        },
        changeSwiper(e) {
            this.current = e.detail.current
        },
        changeTab(value) {
            this.active = value
            this.current = value
        },
        changeItem(item, key) {
            this.$set(item, "active", key)
        },
        async give(val) {
            this.$emit("give", val)
        }
    }
}
</script>

<style lang="scss" scoped>
.popup-body {
    background: #010a16;
}
.rank {
    display: flex;
    align-items: center;
    margin: 20rpx;
    border-radius: 10rpx;
    background: #010a16;
    padding: 20rpx 20rpx 30rpx 20rpx;
    &-title {
        width: 90rpx;
        color: #e81093;
        line-height: 38rpx;
        margin-right: 20rpx;
        padding-top: 10rpx;
        text-align: center;
    }
    &-item {
        position: relative;
        width: 72rpx;
        height: 72rpx;
        margin-right: 36rpx;
    }
    &-avatar {
        width: 100%;
        height: 100%;
        border-radius: 50%;
    }
    &-sex {
        position: absolute;
        width: 60%;
        left: 50%;
        bottom: -10rpx;
        transform: translateX(-50%);
        height: 28rpx;
        line-height: 28rpx;
        background: #fff;
        padding: 0 6px;
        border-radius: 28rpx;
        font-size: 22rpx;
        text-align: center;
    }
    &-level {
        position: absolute;
        right: -30rpx;
        top: 0;
        height: 24rpx;
        line-height: 24rpx;
        border-radius: 6rpx;
        padding: 0 4rpx;
        font-size: 20rpx;
        background: linear-gradient(to right bottom, #ebac4c, #fcd29f);
        color: #fff;
    }
}
.tab {
    height: 80rpx;
    display: flex;
    padding: 0 10rpx;
    margin-bottom: 20rpx;
    &-left {
        flex: 1;
        display: flex;
    }
    &-item {
        margin: 0 20rpx;
        display: flex;
        align-items: center;
        color: #999;
        font-size: 26rpx;
        border-bottom: 2px solid transparent;
        transition: all 0.3s;
        &.tab-active {
            font-size: 30rpx;
            color: #fff;
            border-bottom-color: #fff;
        }
    }
    &-right {
        display: flex;
        align-items: center;
        color: #fff;
        line-height: 28rpx;
    }
    .zuanshi-icon {
        margin-right: 10rpx;
    }
    .arrow-icon {
        margin-left: 20rpx;
        color: #9d6a3e;
    }
}
.zuanshi-icon {
    width: 32rpx;
    height: 24rpx;
}
.swiper {
    height: 700rpx;
    .scroll {
        height: 700rpx;
        padding: 0 20rpx;
        box-sizing: border-box;
    }
    .list {
        display: flex;
        flex-wrap: wrap;
        &-item {
            display: flex;
            flex-direction: column;
            height: 268rpx;
            width: 25%;
            margin-bottom: 20rpx;
            &.active {
                border-radius: 10rpx;
                background: rgba($color: #999, $alpha: 0.2);
                overflow: hidden;
                .list-item__body {
                    padding: 0;
                }
                .list-item__name {
                    display: none;
                }
                .give-btn {
                    display: block;
                    height: 50rpx;
                    text-align: center;
                    line-height: 50rpx;
                    background: linear-gradient(to right bottom, #fc4783, #fd0652);
                    color: #fff;
                    margin-top: 10rpx;
                }
                .list-item__img {
                    animation: animations 2.3s ease-out infinite;
                }
            }
            .give-btn {
                display: none;
            }
            &__head {
                display: flex;
                align-items: center;
                justify-content: center;
                height: 160rpx;
                text-align: center;
                margin-bottom: 10rpx;
            }
            &__body {
                flex: 1;
            }
            &__img {
                height: 120rpx;
            }
            &__name {
                white-space: nowrap;
                text-overflow: ellipsis;
                overflow: hidden;
                text-align: center;
                color: #fff;
                margin-bottom: 10rpx;
            }
            &__footer {
                display: flex;
                justify-content: center;
                align-items: center;
                color: #999;
            }
            .zuanshi-icon {
                color: $purple;
                margin-right: 10rpx;
            }
        }
    }
}
.panel {
    &-item {
        display: flex;
        border-radius: 10rpx;
        height: 140rpx;
        overflow: hidden;
        margin-bottom: 20rpx;
    }
    &-img {
        position: relative;
        top: 0rpx;
        width: 92rpx;
        height: 92rpx;
        transform-origin: 50% 0;
    }
    &-left {
        display: flex;
        align-items: center;
        justify-content: center;
        flex: 1;
        background: rgba($color: #999, $alpha: 0.2);
    }
    &-right {
        width: 200rpx;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        opacity: 0.5;
        background: linear-gradient(to right bottom, #f14090, #ee2f5e);
    }
    .receive {
        display: flex;
        align-items: center;
        line-height: 36rpx;
        margin-bottom: 10rpx;
    }
    .receive-price {
        color: #fff;
        font-size: 36rpx;
        font-weight: bold;
        margin-left: 10rpx;
    }
    .receive-text {
        font-size: 30rpx;
        color: #f5f5f5;
    }
    .active {
        .panel-img {
            animation: animations2 1s linear infinite;
        }
        .panel-right {
            opacity: 1;
        }
    }
}

@keyframes animations {
    0% {
        transform: scale(1.3);
    }
    20% {
        transform: scale(1);
    }
    40% {
        transform: scale(1.2);
    }
    60% {
        transform: scale(1);
    }
    80% {
        transform: scale(1.1);
    }
    90% {
        opacity: 1;
        transform: scale(1);
    }
    100% {
        opacity: 0;
    }
}
@keyframes animations2 {
    0% {
        transform: rotate(15deg);
    }
    25% {
        transform: rotate(-10deg);
    }
    50% {
        transform: rotate(5deg);
    }
    75% {
        transform: rotate(-5deg);
    }
    100% {
        transform: rotate(0deg);
    }
}
</style>
